package jiaerc.moreingredients.item;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;

import jiaerc.moreingredients.MoreIngredientsGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class CabbageSeeds extends Item{
    public final Block cropBlock;
    public CabbageSeeds(Block cropBlock) {
        super(new Properties().tab(MoreIngredientsGroup.MORE_INGREDIENTS_GROUP));
        this.cropBlock = cropBlock;}

    @Override
    public ActionResultType useOn(ItemUseContext context){
        World world = context.getLevel();
        BlockPos pos = context.getClickedPos();
        PlayerEntity player = context.getPlayer();
        ItemStack stack = context.getItemInHand();
        //仅允许在耕地上种植
        if (world.getBlockState(pos).getBlock() == Blocks.FARMLAND && context.getClickedFace() == Direction.UP && player != null) {
            BlockPos cropPos =  pos.above();
            //消耗种子
            if(cropBlock.defaultBlockState().canSurvive(world,cropPos)){
                world.setBlock(cropPos,cropBlock.defaultBlockState(),3);
                stack.shrink(1);
                return ActionResultType.SUCCESS;
            }
        }
        return ActionResultType.PASS;
    }

}
